/******************************************************************************
*  COPYRIGHT MorningCore CO.,LTD
******************************************************************************/

/*****************************************************************
* FileName::    <PUSCHPrecoding.c>
* version::     <1.0.0>
* Purpose:      <do pusch precoding, i.e hybrid radix dft>
* Authors:      <guohuayong, 2008-12-16>
* Notes:        <no>
******************************************************************/

/******************************************************************************
*  HISTORY OF CHANGES
*   <Date>          <Author>        <Version>       <DESCRIPTION>
*   ...             ...              ...            ....
******************************************************************************/

/******************************************************************************
*  INCLUDE FILES

******************************************************************************/

/******************************************************************************
*  INCLUDE FILES

******************************************************************************/
#include "stdlib.h"
#include "../head/ue_tx_pusch_remapper_fix.h"
#include "../head/Ue_Tx_FilePrintf.h"

/*----------- function definition-----------------------------------*/
/*****************************************************************************
* Function: PUSCH_Precoding()
* Purpose:  This function performs precoding processing
*
* Relation:
Calls       : ....
Called by   : ....
* Params:
*   <Name>          <Type>      <In/Out>    <Description>
 LayerNum       int8      In    number of Layers
 PUSCH_TxPortNum    int8      In    number of TxPorts
 CodebookIndex      int8      In    index of codebook
 PUSCH_RB_Num   int8      In    RB number of PUSCH
 PUSCH_SymNum   int8      In    number of SC-FDMA symbols for PUSCH
 DataOp[4][12][1320]  cmplx_frN   In/Out  output of Data
 DMRSOp[4][2][1320]     cmplx_frN   In/Out  output of DMRS
* Return:  void
* Note:    add by lixiaojiao for R11 PUSCH Precoding@2013-12-19
*******************************************************************************/
void PUSCH_REMapper_ReFlag_fix
(
    stULParamTable *SimParam,
    fractN BetaPUSCH,
    fractM BetaDMRS,
    cmplx_frN *P_DataIp[MAXSYMNUM],
    cmplx_frN *P_DMRS_Ip[RS_NUM_PER_TTI_UL], /**/
    cmplx_frN *P_DataOp[MAXSYMNUM]
)
{
    int l, m, k;
    int iRe, MaxScNum, data_symb_idx, dmrs_symb_idx, dmrs_sc, data_sc, iPRB, iPRB_iRe, iPRB_DMRS, iPRB_DMRS_iRe;
    int DMRS_ValidPRB[MAXRBNUM];
    int DMRSCDM_SameGroup = 0;

    MaxScNum = SimParam->PUSCH_RB_Num * SC_PER_RB;

    // init
    for (l = 0; l < MAXSYMNUM; l++)
    {
        for (k = 0; k < PUSCH_Max_RE; k++)
        {
            P_DataOp[l][k].re = 0;
            P_DataOp[l][k].im = 0;
        }
    }

    m = 0;
    for (iPRB = 0; iPRB < MAXRBNUM; iPRB++)
    {
        if (1 == SimParam->RB_BitMap[iPRB]) // SimParam->RB_BitMap = 1
        {
            DMRS_ValidPRB[m] = iPRB;
            m++;
        }
    }
    //
    data_symb_idx = 0;
    dmrs_symb_idx = 0;
    for (l = 0; l < MAXSYMNUM; l++)
    {
		if(SimParam->u8_DMRS_Pos[l] == 0) // PUSCH Data
        {          
			for (m=0;m<SimParam->PUSCH_RB_Num;m++)//useful RB
			{
				iPRB = (SimParam->PUSCH_RB_Idx[m] + SimParam->PUSCH_RB_Offset[l]) % SimParam->SCS_RbNum[SimParam->UeScsIdx];

				for (k=0;k<SC_PER_RB;k++)    
				{
					iPRB_iRe = iPRB*SC_PER_RB+k;
					iRe = m*SC_PER_RB+k;

					P_DataOp[l][iPRB_iRe] = P_DataIp[data_symb_idx][iRe];			
                    vFilePrintDataRemap_kPrint(iRe);//
				}/* end for k */				
			}/* end for m(frequence) */

			data_symb_idx++;
        } // end for if(SimParam->u8_DMRS_Pos[l] == 0) // PUSCH Data
        else if (SimParam->u8_DMRS_Pos[l] == 1) //DMRS
        {
            data_sc = 0;
            dmrs_sc = 0;
            for (m = 0; m < SimParam->PUSCH_RB_Num; m++) //useful RB
            {
                iPRB = (SimParam->PUSCH_RB_Idx[m] + SimParam->PUSCH_RB_Offset[l]) % SimParam->SCS_RbNum[SimParam->UeScsIdx];// added by shanshan @20180928 for HopMod = 1
                iPRB_DMRS = DMRS_ValidPRB[m];

                for (k = 0; k < SC_PER_RB; k++)
                {
                    iRe = m * SC_PER_RB + k;
					iPRB_iRe = iPRB * SC_PER_RB + k;
                    if (SimParam->pusch_re_flag[l][iRe] < 4) // -2: CSI2 punctring by ACK, -1: Data punctring by ACK, 0: UL-SCH, 1: ACK, 2:CSI1, 3: CSI2, 4: DMRS, 5: NULL, 6: SRS, 7:PT-RS
                    {
                        P_DataOp[l][iPRB_iRe] = P_DataIp[data_symb_idx][iRe];
                    }
                    else if (SimParam->pusch_re_flag[l][iRe] == 4) // 0: DMRS
                    {
                        iPRB_DMRS_iRe = iPRB_DMRS * SC_PER_RB + k;

                        P_DataOp[l][iPRB_iRe].re = P_DMRS_Ip[dmrs_symb_idx][iPRB_DMRS_iRe].re;
                        P_DataOp[l][iPRB_iRe].im = P_DMRS_Ip[dmrs_symb_idx][iPRB_DMRS_iRe].im;
                        dmrs_sc++;
                    }
                    else
                    {
                        ;//
                    }
                }
            }

            dmrs_symb_idx++;
        }
        else //
        {
            ;//
        }
    }/* end for l(time) */
    return;
}



